<?php
declare(strict_types=1);

// 导航高亮显示
function nav_active(string $nav, string $class = ' class="active"'): string
{
    if (str_starts_with($nav, '@')) {
        $nav = substr($nav, 1);
        return ($nav == get_action()) ? $class : '';
    }
    return ($nav == get_controller()) ? $class : '';
}

// form-select生成
function form_select(string $name, $options, $selected = '', string $attr = ''): string
{
    if (!is_array($options)) {
        $options = str_to_array($options);
    }
    if (!empty($attr)) {
        $attr = ' ' . clear_html($attr);
    }
    $selected = is_array($selected) ? $selected : explode(',', strval($selected));
    $select = '<select name="' . $name . '"' . $attr . '>' . "\n";
    foreach ($options as $k => $v) {
        $select .= "\t<option value=\"{$k}\"" . (in_array($k, $selected) ? ' selected="selected"' : '') . ">{$v}</option>\n";
    }
    return $select . "</select>\n";
}

// form-radio生成
function form_radio(string $name, $options, $selected = '', string $attr = '', bool $is_label = false): string
{
    if (!is_array($options)) {
        $options = str_to_array($options);
    }
    if (!empty($attr)) {
        $attr = ' ' . clear_html($attr);
    }
    $radio = '';
    foreach ($options as $k => $v) {
        if ($is_label) {
            $radio .= '<label' . $attr . '><input type="radio" name="' . $name . '" value="' . $k . '"' . ($k == $selected ? ' checked="checked"' : '') . ' />' . $v . '</label>' . "\n";
        } else {
            $radio .= '<input type="radio" name="' . $name . '" value="' . $k . '" title="' . $v . '"' . ($k == $selected ? ' checked="checked"' : '') . ' />';
        }
    }
    return $radio;
}

// 格式化输出IP
function ip_format(int $ip): string
{
    return preg_replace('/(\d+)\.(\d+)\.(\d+)\.(\d+)/i',"$1.$2.*.$4", long2ip($ip));
}


// 获取随机缩略图
function rand_thumb(string $url, int $i): string
{
    if (!empty($url)) {
        return $url;
    }
    if ($i > 40) {
        $i = $i % 40;
    }
    return __STATIC__ . '/img/thumb/' . $i . '.jpg';
}

// 获取表数据文档上下篇
function get_related_table(int $id, string $table, array $options = []): array
{
    $data = widget('common.related')->get($table, $options);
    return get_related($id, $data);
}

// 获取文档上下篇
function get_related_book(int $id, int $book_id): array
{
    $data = widget('common.doc_sort')->get($book_id);
    return get_related($id, $data);
}

// 根据数据获取上下篇
function get_related(int $id, array $data): array
{
    [$prev_id, $next_id] = get_prev_next($id, array_keys($data));
    $prev = $prev_id ? ['id' => $prev_id, 'title' => $data[$prev_id]] : [];
    $next = $next_id ? ['id' => $next_id, 'title' => $data[$next_id]] : [];
    return ['prev' => $prev, 'next' => $next];
}

// 封面上传表单属性
function pic_attr(string $url = ''): array
{
    $data = [
        'img' => ' style="display:none"',
        'span' => '',
    ];
    if (!empty($url)) {
        $data['img'] = ' src="'.$url.'"';
        $data['span'] = ' style="display:inline"';
    }
    return $data;
}

// 获取用户昵称
function get_nickname(int $id): string
{
    $user = db('user')->where('status=1')->cache()->column('nickname', 'id');
    return $user[$id] ?? '无名';
}

// 获取文集名称
function get_book_name(int $id): string
{
    $book = db('book')->cache()->column('title', 'id');
    return $book[$id] ?? '无文集名';
}

// 获取文档起启ID
function get_start_id(int $book_id): int
{
    $doc = widget('common.doc_sort')->get($book_id);
    if (!empty($doc)) {
        return array_key_first($doc);
    }
    return 0;
}

// 获取文档所属章节号
function get_chapter(int $id, int $book_id): string
{
    $data = widget('common.doc_sort')->get($book_id);
    $keys = array_keys($data);
    return str_pad(strval(array_search($id, $keys) + 1), 2, '0', STR_PAD_LEFT);
}

// 获取深色样式
function dark_css(string $css = ' class="theme-dark"'): string
{
    $currentHour = date('H');
    $isNight = ($currentHour >= 18 || $currentHour <= 6);
    $cookie = cookie('theme-dark');
    if (!$cookie && $isNight) {
        return $css;
    }
    return $cookie == 'true' ? $css : '';
}

// 获取文集权限 0. 无权限 1. viewer 2 editor 3 owner || 超管
function get_prem_level(int $id, bool $is_book_id = true): int
{
    if (!$is_book_id) {
        $id = db('doc')->where('id', $id)->cache()->value('book_id');
    }
    $user = session('user');
    if (!$user) {
        return 0;
    }
    if ($user['level'] >=  3) {
        return 3;
    }
    $member = db('book_member')->where('book_id', $id)->where('status=1')->cache()->column('role_level', 'user_id');
    return $member[$user['id']] ?? 0;
}

// 验证权限
function check_prem_level(int $level, int $book_id): bool
{
    $prem_level = get_prem_level($book_id);
    return $prem_level >= $level;
}

// 获取角色名
function get_role(int $role_level): string
{
    $role = [1 => 'viewer',2 => 'editor',3 => 'owner'];
    return $role[$role_level] ?? '无';
}

// 获取成员待审核数
function get_member_count(bool $badge = true): string
{
    $member_count = widget('common.member_count')->get();
    $html = $badge ? '<span class="badge">'.$member_count.'</span>' : strval($member_count);
    return $member_count > 0 ? $html : '';
}

// 根据文档ID获取文档标题
function get_doc_title(int $doc_id): string
{
    $data = db('doc')->where('id', $doc_id)->cache()->column('title', 'id');
    return $data[$doc_id] ?? '文档不存在';
}